{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bc4d6f2e",
   "metadata": {},
   "source": [
    "# 数字类型\n",
    "Python 共有三种数字类型：\n",
    "\n",
    "int\n",
    "\n",
    "float\n",
    "\n",
    "complex\n",
    "\n",
    "数值类型的变量是在你给它们赋值时创建的："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8baae956",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = 1    # int\n",
    "y = 2.8  # float\n",
    "z = 1j   # complex"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "128689b3",
   "metadata": {},
   "source": [
    "要验证 Python 中任何对象的类型，请使用 type() 函数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "701a5920",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'>\n",
      "<class 'float'>\n",
      "<class 'complex'>\n"
     ]
    }
   ],
   "source": [
    "print(type(x))\n",
    "print(type(y))\n",
    "print(type(z))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc7a08f5",
   "metadata": {},
   "source": [
    "# Int\n",
    "Int 或 integer，是一个整数，正负，不带小数，长度不限。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "cce7b2cc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'>\n",
      "<class 'int'>\n",
      "<class 'int'>\n"
     ]
    }
   ],
   "source": [
    "x = 1\n",
    "y = 35656222554887711\n",
    "z = -3255522\n",
    "\n",
    "print(type(x))\n",
    "print(type(y))\n",
    "print(type(z))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "891fbaed",
   "metadata": {},
   "source": [
    "# Float\n",
    "浮点数是包含一位或多位小数的正数或负数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5f850e20",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'float'>\n",
      "<class 'float'>\n",
      "<class 'float'>\n"
     ]
    }
   ],
   "source": [
    "x = 1.10\n",
    "y = 1.0\n",
    "z = -35.59\n",
    "\n",
    "print(type(x))\n",
    "print(type(y))\n",
    "print(type(z))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "789da8e0",
   "metadata": {},
   "source": [
    "浮点数也可以是带有“e”的科学数字，表示 10 的幂。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d91713e6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'float'>\n",
      "<class 'float'>\n",
      "<class 'float'>\n"
     ]
    }
   ],
   "source": [
    "x = 35e3\n",
    "y = 12E4\n",
    "z = -87.7e100\n",
    "\n",
    "print(type(x))\n",
    "print(type(y))\n",
    "print(type(z))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ceb1d7f8",
   "metadata": {},
   "source": [
    "# 复数\n",
    "复数写有“j”作为虚部："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "94a53e25",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'complex'>\n",
      "<class 'complex'>\n",
      "<class 'complex'>\n"
     ]
    }
   ],
   "source": [
    "x = 3+5j\n",
    "y = 5j\n",
    "z = -5j\n",
    "\n",
    "print(type(x))\n",
    "print(type(y))\n",
    "print(type(z))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "128d3675",
   "metadata": {},
   "source": [
    "# 类型转换\n",
    "您可以使用 int()、float() 和 complex() 方法从一种类型转换为另一种类型："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "eb2b56cd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.0\n",
      "2\n",
      "(1+0j)\n",
      "<class 'float'>\n",
      "<class 'int'>\n",
      "<class 'complex'>\n"
     ]
    }
   ],
   "source": [
    "x = 1    # int\n",
    "y = 2.8  # float\n",
    "z = 1j   # complex\n",
    "\n",
    "#convert from int to float:\n",
    "a = float(x)\n",
    "\n",
    "#convert from float to int:\n",
    "b = int(y)\n",
    "\n",
    "#convert from int to complex:\n",
    "c = complex(x)\n",
    "\n",
    "print(a)\n",
    "print(b)\n",
    "print(c)\n",
    "\n",
    "print(type(a))\n",
    "print(type(b))\n",
    "print(type(c))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20a0d431",
   "metadata": {},
   "source": [
    "# 随机数\n",
    "Python 没有 random() 函数来生成随机数，但 Python 有一个名为 random 的内置模块，可用于生成随机数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e504c5a1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "\n",
    "print(random.randrange(1, 10))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
